﻿/// <reference path="../jquery-1.9.1.js" />
/// <reference path="../referraltracker.js" />

referraltracker.dialogs.sitedialog = function () {

    var _defaults = {
        name: "",
        url: "",
        pattern: "",
        mode: "add"
    };
    var _settings = {};
    var _container = {};
    var _onsave = null;
    var _oncancel = null;

    function Initialize(oninit) {
        if ($.isEmptyObject(_container)) {
            $.get("/scripts/admin/sitedialog.ui.htm", function (data) {
                _container = $(data);
                $("body").append(_container);
                BindEvents();
                oninit();
            });
        } else {
            oninit();
        }
    }

    function InitDialog() {
        var title = $("#sitedialog_title");

        if (_settings.mode == "add") {
            title.text("Add New Site");
        } else {
            title.text("Update Site");
        }

        var name = $("#sitedialog_name");
        var url = $("#sitedialog_domain");
        var pattern = $("#sitedialog_pattern");

        name.val(_settings.name);
        url.val(_settings.url);
        pattern.val(_settings.pattern);
        
    }

    function BindEvents() {
        $("#sitedialog_ok").click(function () {
            _onsave(GetData());
            _container.hide();
        });

        $("#sitedialog_cancel").click(function () {
            _container.hide();
        });
    }

    function GetData() {
        var formData = {
            name: $("#sitedialog_name", _container).val(),
            url: $("#sitedialog_domain", _container).val(),
            pattern: $("#sitedialog_pattern", _container).val()
        };

        return formData;
    }

    function ShowDialog(data) {
        $.extend(_settings, _defaults, data || {});
        InitDialog();
        _container.show();
    }

    var _instance = {
        initialize: function (oninit) {
            Initialize(oninit);
        },
        show: ShowDialog,
        getData: function () {
            return GetData();
        },
        onSave: function (onsave) {
            _onsave = onsave;
        }
    };

    return _instance;
};